package gw.com.android.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.gwtsz.gts2.hx.R;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import gw.com.android.app.AppMain;


/**
 * Created by aries.zhan on 2017/3/17.
 * 获取渠道信息类
 */

public class ChannelUtil {

    private static final String CHANNEL_KEY = "cztchannel";
    /**
     * 获取渠道名
     * @param context
     * @return
     */
    public String getChannel(Context context){
        String mChannel = "";
        //sp 中获取
        mChannel = getChannelBySharedPreferences(context);
        if (!TextUtils.isEmpty(mChannel)){
            return mChannel;
        }
        //从apk中获取
        mChannel = getChannelFromApk(context, CHANNEL_KEY);
        if (!TextUtils.isEmpty(mChannel)){
            saveChannelBySharedPreferences(context,mChannel);
            return mChannel;
        }
        return context.getString(R.string.umeng_default);
    }

    /**
     * 获取渠道标签名
     */
    public String getChannelTag(){
        String channel = getChannel(AppMain.getApp());
        if ("GTS2_xiaomi&utm_medium=yysd".equals(channel)) {
            return "GTS2_xiaomi";
        } else if ("GTS2_vivo&utm_medium=yysd".equals(channel)) {
            return "GTS2_vivo";
        } else if ("GTS2_oppo&utm_medium=yysd".equals(channel)) {
            return "GTS2_oppo";
        }
        return null;
    }

    /**
     * 从sp中获取channel
     * @param context
     * @return 返回获取到的channel
     */
    private String getChannelBySharedPreferences(Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(CHANNEL_KEY,"");
    }

    /**
     * 本地保存channel & 对应版本号
     * @param context
     * @param channel
     */
    private static void saveChannelBySharedPreferences(Context context, String channel){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(CHANNEL_KEY, channel);
        editor.commit();
    }

    /**
     * 从apk中获取版本信息
     * @param context
     * @param channelKey
     * @return
     */
    private static String getChannelFromApk(Context context, String channelKey) {
        //从apk包中获取
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        //默认放在meta-inf/里， 所以需要再拼接一下
        String key = "META-INF/" + channelKey;
        String ret = "";
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.startsWith(key)) {
                    ret = entryName;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String[] split = ret.split("_");
        String channel = "";
        if (split != null && split.length >= 2) {
            channel = ret.substring(split[0].length() + 1);
        }
        return channel;
    }
}
